package org.nativejpa.samples.firstapp;

import javax.persistence.Id;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.Column;
import javax.persistence.OneToOne;
import javax.persistence.JoinColumn;
import java.util.Date;

/**
 * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
 * @create 2009-8-19 14:36:51
 */
@Entity
@Table(name = "PERSON")
@NamedNativeQueries(
        {
                @NamedNativeQuery(name = PersonEntity.NAMED_QUERY_CREATE_TABLE,
                        query = "CREATE TABLE PERSON(ID BIGINT NOT NULL, NAME VARCHAR(50) NOT NULL, AGE INT, MARRIED CHAR(1), BIRTH_DATE DATE, DESCRIPTION CLOB, ADDRESS_ID BIGINT)"),
                @NamedNativeQuery(name = PersonEntity.NAMED_QUERY_SELECT_BY_CONDITION,
                        query = "SELECT * FROM PERSON WHERE ADDRESS_ID = $ADDRESS_ID",
                        resultClass = PersonEntity.class)

        }
)
public class PersonEntity {

    public static final String NAMED_QUERY_CREATE_TABLE = "PERSON.create_table";
    public static final String NAMED_QUERY_INSERT_ONE = "PERSON.insert_one";
    public static final String NAMED_QUERY_DELETE_ONE = "PERSON.delete_one";
    public static final String NAMED_QUERY_UPDATE_ONE = "PERSON.update_one";
    public static final String NAMED_QUERY_SELECT_ONE = "PERSON.select_one";
    public static final String NAMED_QUERY_SELECT_ALL = "PERSON.select_all";
    public static final String NAMED_QUERY_SELECT_BY_CONDITION = "PERSON.select_by_condition";

    @Id
    @Column(name = "ID")
    private long Id;

    @Column(name = "NAME")
    private String name;

    @Column(name = "AGE")
    private int age;

    @Column(name = "MARRIED")
    private boolean married;
    
    @Column(name = "BIRTH_DATE")
    private Date birthDate;
    
    @Column(name = "DESCRIPTION")     // big TEXT
    private String description;

    //TODO: auto fetch mapping object by specify SelectSQL or by columnDefinition
    @JoinColumn(name = "ADDRESS_ID", referencedColumnName = "ID", table = "ADDRESS")
//    @JoinColumn(name = "ADDRESS", columnDefinition = "SELECT * FROM ADDRESS WHERE ID = $ID")
    private AddressEntity address;

    public long getId() {
        return Id;
    }

    public void setId(long id) {
        Id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public boolean isMarried() {
        return married;
    }

    public void setMarried(boolean married) {
        this.married = married;
    }

    public Date getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public AddressEntity getAddress() {
        return address;
    }

    public void setAddress(AddressEntity address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName() + "{" + "id=" + this.getId() + ", name=" +this.getName()+"}";
    }
}
